package com.example.demo.action;

import com.example.demo.dao.ToolUserMapper;
import com.example.demo.pojo.Tool;
import com.example.demo.pojo.tool_Admin;
import com.example.demo.pojo.tool_User;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Service
@Controller
@RequestMapping("/tooluser")
public class ToolUserAction {

    @Autowired
    ToolUserMapper toolUserMapper;



    @RequestMapping("/logina")
    public String logins(String user_nameCode,String user_passwordCode, HttpSession session) {
        System.out.println(user_nameCode+ "----" + user_passwordCode);
        tool_User u=toolUserMapper.findToolUser(user_nameCode,user_passwordCode);
        if(u!=null){
            session.setAttribute("userinfo",u);
            return "welcomeuser";
        }

        return "userlogin";
    }


    @RequestMapping(value = "/tools")
    public String findAll(Model model,
                          @RequestParam(value = "pageNow", required = false, defaultValue = "1") Integer pageNow,
                          @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize) {
        PageHelper.startPage(pageNow, pageSize);
        List<Tool> tools = toolUserMapper.selectAlltool();
        PageInfo<Tool> pageInfo = new PageInfo<>(tools);
        if (pageNow == 1) {
            pageInfo.setPrePage(1);
        }
        if (pageInfo.getNextPage() == 0) {
            pageInfo.setNextPage(pageInfo.getPrePage() + 1);
        }
        model.addAttribute("tools",tools );
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("pageNow", pageNow);
//        try {
//            Thread.sleep(3000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//        System.out.println("我睡了3秒……");
        return "welcomeuser";
    }


    @RequestMapping(value = "/select")
    public String findByCode(Model model, String tool_sortCode,
                             @RequestParam(value = "pageNow", required = false, defaultValue = "1") Integer pageNow,
                             @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize) {
        PageHelper.startPage(pageNow, pageSize);
        List<Tool> tools = toolUserMapper.selectBySort(tool_sortCode);
        PageInfo<Tool> pageInfo = new PageInfo<>(tools);
        if (pageNow == 1) {
            pageInfo.setPrePage(1);
        }
        if (pageInfo.getNextPage() == 0) {
            pageInfo.setNextPage(pageInfo.getPrePage() + 1);
        }
        model.addAttribute("tools", tools);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("pageNow", pageNow);
        return "welcomeuser";
    }

    @RequestMapping(value = "edit/{tool_id}")
    @ResponseBody
    public Tool findToolById(@PathVariable String tool_id) {
        System.out.println("这里是获取到的id"+tool_id);
        Tool tool = toolUserMapper.selectByKey(tool_id);
        return tool;
    }

    @PutMapping("/edit")
    public String editTool(Tool tool) {
        toolUserMapper.editTool(tool);
        return "welcomeuser";
    }


}
